package com.example.mymvvmdemo.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationUtils {

    //验证手机号格式
    public static boolean phoneValidation(String phone) {
        if (StrUtils.isEmpty(phone))
            return false;
        String pattern = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(phone);
        return m.matches();
    }

    //字母验证
    public static boolean letterValidation(String phone) {
        if (StrUtils.isEmpty(phone))
            return false;
        String pattern = "^([\\u4E00-\\u9FA5a-zA-Z]+)$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(phone);
        return m.matches();
    }

    //验证密码格式
    public static boolean passwordValidation(String password) {
        if (StrUtils.isEmpty(password))
            return false;
        String pattern = "^(?![\\d]+$)(?![a-z]+$)(?![A-Z]+$)(?![~!@#$%^&*._]+$)[\\da-zA-z~!@#$%^&*._]{8,32}$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(password);
        return m.matches();
    }

    //验证昵称格式 允许空格
    public static boolean nickNameValidation(String nickName) {
        if (StrUtils.isEmpty(nickName))
            return false;
        String pattern = "^[\\u4E00-\\u9FA5A-Za-z0-9 ]+$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(nickName);
        return m.matches();
    }

    //邮箱格式判断
    public static boolean emailValidation(String email) {
        if (StrUtils.isEmpty(email))
            return false;
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    //验证设备ip格式
    public static boolean ipValidation(String ip) {
        if (StrUtils.isEmpty(ip))
            return false;
        String pattern = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|[1-9])\\."
                + "(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)\\."
                + "(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)\\."
                + "(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)"; //限定输入格式

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(ip);
        return m.matches();
    }

    //分组名称判断 , (1-32位数字，中英文)
    public static boolean groupNameValidation(String email) {
        if (StrUtils.isEmpty(email))
            return false;
        String str = "^([a-zA-Z0-9\\u4e00-\\u9fa5]{1,32})$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }


    //密码 , (8-32位数字，中英文)
    public static boolean passWordValidation(String email) {
        if (StrUtils.isEmpty(email))
            return false;
        String str = "^([a-zA-Z0-9\\u4e00-\\u9fa5]{8,32})$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean isEmail(String input) {
        return input.contains("@") || input.contains(".");
    }

    public static boolean deviceName(String input) {
        if (StrUtils.isEmpty(input)) {
            return false;
        }
        String str = "^[\\u4E00-\\u9FFFa-zA-Z0-9\\s]+$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(input);
        return m.matches();
    }

    /**
     * 中英文当都在1-10
     * @param input
     * @return
     */
    public static boolean checkUserName(String input) {
        if (StrUtils.isEmpty(input)) {
            return false;
        }
        String str = "^[\\u4E00-\\u9FFFa-zA-Z0-9\\s]{1,10}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(input);
        return m.matches();
    }

}
